package gui.productgroup;

import core.ContainerFacade;
import core.exception.ModelException;
import gui.common.*;
import gui.inventory.*;

/**
 * Controller class for the edit product group view.
 */
public class EditProductGroupController extends Controller 
										implements IEditProductGroupController {
	
	
	ProductContainerData _target;
	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit product group view
	 * @param target Product group being edited
	 */
	public EditProductGroupController(IView view, ProductContainerData target) {
		super(view);
		
		_target = target;

		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductGroupView getView() {
		return (IEditProductGroupView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		// There are 4 components that can be enabled/disabled.
		// The only one we care about is the okay button.
		getView().enableOK(ContainerFacade.instance().canRenameContainer(
				_target.getContainer(), getView().getProductGroupName()) &&
				ContainerFacade.instance().canSetThreeMonthSupply(
						_target.getContainer(), 
						getView().getSupplyValue(), 
						getView().getSupplyUnit().toString())
				);		
		
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		getView().setProductGroupName(_target.getName());
		getView().setSupplyValue(
				ContainerFacade.instance().get3MonthSupplyQuantity(_target.getContainer()));
		
		SizeUnits unit = SizeUnits.fromString(
				ContainerFacade.instance().get3MonthSupplyUnit(_target.getContainer()));
		getView().setSupplyUnit(unit);
	}

	//
	// IEditProductGroupController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product group view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product group view.
	 */
	@Override
	public void editProductGroup() {		
		try {
			if (!getView().getProductGroupName().equals(_target.getName()))
				ContainerFacade.instance().renameContainer(
					_target.getContainer(), getView().getProductGroupName());
			ContainerFacade.instance().setThreeMonthSupply(
					_target.getContainer(), 
					getView().getSupplyValue(), 
					getView().getSupplyUnit().toString());
		} catch (ModelException e) {
			getView().displayErrorMessage(e.getMessage());
		}
			
	}

}

